package cn.wolfcode.p2p.base.util;


import org.apache.commons.lang3.time.DateUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateUtil {


    public static Date string2date(String date, String pattern) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(pattern);
            return sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 得到一天的最后一秒钟
     */
    public static Date getEndDate(Date now) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(now);
        calendar.add(Calendar.HOUR_OF_DAY, 23);
        calendar.add(Calendar.MINUTE, 59);
        calendar.add(Calendar.SECOND, 59);
        return calendar.getTime();
    }

    /**
     * 两个时间的间隔秒
     */
    public static int getBetweenTime(Date date, Date other) {
        return (int) (Math.abs(date.getTime() - other.getTime()) / 1000);
    }

    public static List<Date> getLastDays(Date date, int number) {
        ArrayList<Date> list = new ArrayList<>(number);
        for (int i = 1; i <= number; i++) {
            Date day = DateUtils.addDays(date, i);
            list.add(day);
        }
        return list;
    }
}